import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_syt/api/api.dart';
import 'package:flutter_syt/application.dart';
import 'package:flutter_syt/datas/user_info.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/theme/theme_color.dart';
import 'package:flutter_syt/utils/click_utils.dart';
import 'package:provider/provider.dart';
import 'package:yyy_route_annotation/yyy_route_annotation.dart';

@RoutePage(name: "editUserNickNamePage")
class EditUserNickNamePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _EditUserNickNamePageState();
  }
}

class _EditUserNickNamePageState extends State<EditUserNickNamePage> {
  late TextEditingController textController;

  _saveNickName() async {
    UserInfo info =
        await API.shared.user.updateUserInfo('nickname', textController.text);
    UserInfo.update(info.jsonValue);
    Navigator.of(context).pop();
  }

  @override
  void initState() {
    UserInfo me = Provider.of<UserInfo>(context, listen: false);
    textController =
        new TextEditingController(text: S.of(context).user_nickname);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          '设置昵称',
        ),
      ),
      body: Container(
        margin: EdgeInsets.all(20.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              margin: EdgeInsets.only(bottom: 20),
              child: TextField(
                enableInteractiveSelection: true,
                inputFormatters: [
                  FilteringTextInputFormatter.deny(appContext.emojiRegexp)
                ],
                controller: textController,
                autofocus: false,
                maxLength: 15,
                style: TextStyle(
                    color: ThemeColor.colorBlack100, fontSize: 18, height: 1.5),
                cursorColor: ThemeColor.primaryColor,
                decoration: InputDecoration(
                  hintText: '请输入昵称',
                  counterText: '',
                  hintStyle: TextStyle(
                    color: ThemeColor.colorBlack100,
                    fontSize: 18,
                  ),
                  focusColor: ThemeColor.colorBlack100,
                  contentPadding:
                      EdgeInsets.symmetric(vertical: 10, horizontal: 15),
                  enabledBorder: UnderlineInputBorder(
                    borderSide: BorderSide(
                      color: ThemeColor.colorBlack30,
                      width: 0.5,
                    ),
                  ),
                  focusedBorder: UnderlineInputBorder(
                    borderSide: BorderSide(
                      color: ThemeColor.colorBlack30,
                      width: 0.5,
                    ),
                  ),
                  filled: false,
                  helperText: '最多15个字符',
                  // fillColor: ThemeColor.colorBlack10038,
                ),
                // onSaved: (val) => _text = val!,
                obscureText: false,
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 30),
              padding: EdgeInsets.all(10),
              child: ElevatedButton(
                  child: const Text(
                    '确定',
                    style: TextStyle(fontWeight: FontWeight.bold),
                  ),
                  onPressed: ClickUtils.debounce(
                    () {
                      _saveNickName();
                    },
                  )),
            ),
          ],
        ),
      ),
    );
  }
}
